package com.dongbao.action;

import com.alibaba.fastjson.JSON;
import com.dongbao.data.Datacenter;
import com.dongbao.data.MessageData;
import com.dongbao.entity.MqMessage;
import com.dongbao.service.MyMqttClient;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.SelectionModel;

import java.util.Date;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import static com.dongbao.common.MessageConstant.ALL_MESSAGE;

public class PopupAction extends AnAction {

    @Override
    public void actionPerformed(AnActionEvent e) {
        //获取当前编辑器对象
        Editor editor = e.getRequiredData(CommonDataKeys.EDITOR);
        //获取选择的数据模型
        SelectionModel selectionModel = editor.getSelectionModel();
        //获取当前选择的文本
        String selectedText = selectionModel.getSelectedText();
        MqMessage mqMessage=MqMessage.getInstance();
        
        MessageData messageData=new MessageData();
        messageData.setSendIp(Datacenter.clientIp);
        messageData.setSendTime(new Date());
        messageData.setContent(selectedText);

        mqMessage.setData(messageData);
        mqMessage.setCode(ALL_MESSAGE);
        ExecutorService executor = Executors.newSingleThreadExecutor();
        Future<Integer> future = executor.submit(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                Datacenter.myMqttClient.publishMessage(MyMqttClient.TOPIC_PREFIX+MyMqttClient.TOPIC_ALL, JSON.toJSONString(mqMessage),1);
                return 3;
            }
        });

    }
}
